package com.bawei.yangzongcai___chenmei.BaseAddter;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import java.io.IOException;

/**
 * create by ertu
 * create on 2021/7/19
 * description
 */
public class ShipingPlay extends SurfaceView implements SurfaceHolder.Callback {
    MediaPlayer mediaPlayer;
    public ShipingPlay(Context context) {
        super(context);
        inition();
    }

    public ShipingPlay(Context context, AttributeSet attrs) {
        super(context, attrs);
        inition();
    }
    public void inition()
    {
        getHolder().addCallback(this);
        mediaPlayer = new MediaPlayer();

    }

    public void Plays(String upd)
    {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(upd);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        mediaPlayer.setDisplay(holder);
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        if (mediaPlayer!=null)
        {
            mediaPlayer.release();
        }
    }
}
